<template>
	<view>
		<view class="top">
		</view>

		<uni-card style="position: relative; top: -280rpx; border-radius: 30rpx;">

			<view class="uni-padding-wrap uni-common-mt">
				<uni-segmented-control :current="current" :values="items" :style-type="styleType"
					:active-color="activeColor" @clickItem="onClickItem" class="rounded-segmented-control" />
			</view>

			<view class="content">
				<view v-if="current === 0">
					<view v-for="item in activityList" v-if="item.state==1">
						<view class="activity">
							<view class="left" @click="toDetail(item.id)">
								<view style="font-size: 30rpx; margin-bottom: 20rpx;">{{item.title}}</view>
								<view style="font-size: 26rpx; font-weight: 100;">{{item.createTime}}</view>
							</view>
							<view class="right">
								<u-button type="primary" shape="circle" style="width: 130rpx;height: 55rpx;" text="下架"
									@click="updateState(item)">
								</u-button>
							</view>
						</view>
					</view>


				</view>

				<view v-if="current === 1">
					<view v-for="item in activityList" v-if="item.state==0">
						<view class="activity_false">
							<view class="left" @click="toDetail(item.id)">
								<view style="font-size: 30rpx; margin-bottom: 10rpx;">{{item.title}}</view>
								<view style="color: #ff6840;">
									{{item.type == 0 ? '未审核' : item.type == 1 ? '已审核' : '已拒绝'}}
								</view>
								<view style="font-size: 26rpx; font-weight: 100;">{{item.createTime}}</view>
							</view>
							<view class="right">
								<u-button v-if="item.type==1" type="primary" shape="circle"
									style="width: 130rpx;height: 55rpx; margin-right: 10rpx;" text="上架"
									@click="updateState(item)">
								</u-button>
								<u-button type="primary" shape="circle" style="width: 130rpx;height: 55rpx;" text="删除"
									@click="remove(item)">
								</u-button>
							</view>
						</view>
					</view>


				</view>
			</view>
		</uni-card>


		<u-button @click="toaddActivity" type="primary" text="发布活动"
			style="width: 70%; border-radius: 50px; position: relative;top: -140rpx;"></u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				items: ['已上架', '未上架'],
				colors: ['#007aff', '#4cd964', '#dd524d'],
				current: 0,
				activeColor: '#007aff',
				styleType: 'button',
				activityList: []
			}
		},
		onLoad() {
			this.getList()
		},

		methods: {
			remove(item) {
				let that = item.id;
				let this1 = this;
				this.$http({
					url: '/api/activity/activity/' + that,
					method: 'DELETE',
					success(res) {
						if (res.data.code == 200) {
							this1.getList();
							uni.showToast({
								title: '操作成功',
								duration: 1000,
								icon: 'success',
							});
						} else {
							uni.showToast({
								title: '操作失败，请重新尝试',
								duration: 1000,
								icon: 'error'
							});
						}
					}
				})
			},
			updateState(item) {
				let that = item;
				if (that.state == 1) {
					that.state = 0;
				} else {
					that.state = 1;
				}

				console.log(that)
				this.$http({
					url: '/api/activity/activity',
					method: 'PUT',
					data: that,
					success(res) {
						if (res.data.code == 200) {
							uni.showToast({
								title: '操作成功',
								duration: 1000,
								icon: 'success',
							});
						} else {
							uni.showToast({
								title: '操作失败，请重新尝试',
								duration: 1000,
								icon: 'error'
							});
						}
					}
				})
			},
			getList() {
				let that = this
				this.$http({
					url: '/api/activity/activity/list',
					method: 'GET',
					success(res) {
						that.activityList = res.data.rows
						console.log(that.activityList)
					}
				})
			},
			toDetail(id) {
				console.log(id)
				uni.navigateTo({
					url: '../hjjhuodongdetails/hjjhuodongdetails?id=' + id,
				});
			},
			toaddActivity() {
				uni.navigateTo({
					url: '../hjjhuodongfabu/hjjhuodongfabu'
				});
			},
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			styleChange(e) {
				if (this.styleType !== e.detail.value) {
					this.styleType = e.detail.value
				}
			},
			colorChange(e) {
				if (this.styleType !== e.detail.value) {
					console.log(e.detail.value);
					this.activeColor = e.detail.value
				}
			}
		}
	}
</script>

<style lang="scss">
	.activity_false {

		height: 220rpx;
		box-sizing: border-box;
		width: 100%;
		display: flex;
		margin-top: 40rpx;
		padding: 50rpx 0rpx;
		border-bottom: 3px solid #f7f7f7;
		justify-content: space-between;

		.left {
			color: black;
		}

		.right {
			padding: 45rpx 0px;
			display: flex;
		}
	}

	.activity {

		height: 190rpx;
		box-sizing: border-box;
		width: 100%;
		display: flex;
		margin-top: 40rpx;
		padding: 50rpx 0rpx;
		border-bottom: 3px solid #f7f7f7;
		justify-content: space-between;

		.left {
			color: black;
		}

		.right {
			padding: 35rpx 0px;

		}
	}

	.top {
		padding-right: 20rpx;
		padding-left: 20rpx;
		height: 360rpx;
		background-color: #2f81fa;
		padding-top: 60rpx;
	}

	.uni-common-mt {
		margin-top: 10rpx;
	}

	.uni-padding-wrap {

		width: 350rpx;
		padding: 0px 70px;
	}

	.rounded-segmented-control {
		border-radius: 15rpx;
	}
</style>